Simply manage your HTACCESS file with ease!


//Simple Usage
$htaccess = Array(
Array("rule"=>"^(contact|blog|gallery|about)$", "action"=>"$1.php"),
Array("rule"=>"^(.*?)\.ajax$", "action"=>"_ajax/$1.php")
);
include "Classes/htaccess.class.php";
$ht = new HTACCESS;
$ht->writeRules( $htaccess, true );
$ht->close();


==============================================================

<?php
Class HTACCESS {
	/*
	 * @filename - htaccess.class.php
	 * @Author   - Mark Hughes A.K.A huzi8t9
	 * @Date     - 17th May 2010
	 *
	 */

	protected $resource;

	public    $mode   = "a+"; //Default: a+    | Use "a+" incase file doesn't exist
	public    $rule   = "RewriteRule [RULE] [ACTION] [L]\r\n";

	public function __CONSTRUCT()
	{
		$this->resource = fopen(".htaccess", $this->mode);
	}

	public function writeRules ( $arr, $write_new = false )
	{
		if ( $write_new )
		{
			$clr = $this->_clear();
			fwrite($this->resource, "RewriteEngine On\r\n\r\n");
		}
		for ( $i = 0; $i < count($arr); $i++ )
		{
			$string = $this->rule;
			$string = str_replace("[RULE]", $arr[$i]['rule'], $string);
			$string = str_replace("[ACTION]", $arr[$i]['action'], $string);

			fwrite($this->resource, $string);
		}
	}

	public function addRule($rule, $action)
	{
		$str = str_replace("[RULE]", "$rule", "RewriteRule [RULE] [ACTION] [L]\r\n");
		$str = str_replace("[ACTION]", "$action", $str);
		fwrite($this->resource, $str);
	}

	private function _wipe()
	{
		$string = "";
		$f = fopen(".htaccess", "w");
		fwrite( $f, $string);
		fclose($f);
	}

	public function close()
	{
		fclose( $this->resource );
	}

	private function _clear()
	{
		$unlink = @unlink(".htaccess");
		$try2   = @unlink("&#46;&#46;/.htaccess");
		if ( $unlink )
		{
			return true;
		}
		else
		{
			$this->_wipe();
			return true;
		}
	}
}
?>